<template>
  <div class="login-bg">
    <div class="login-card">
      <div class="login-header">
        <h2>欢迎注册校园综合平台</h2>
        <p class="login-desc">让校园生活更智能、更便捷</p>
      </div>
      <form class="login-form" @submit.prevent="handleRegister">
        <div class="input-group">
          <span class="input-icon"><i class="icon icon-user"></i></span>
          <input
            type="text"
            v-model="nickname"
            placeholder="请输入昵称"
            :class="{ 'input-error': nicknameError }"
            autocomplete="nickname"
          />
        </div>
        <span v-if="nicknameError" class="error-message">{{ nicknameError }}</span>
        <div class="input-group">
          <span class="input-icon"><i class="icon icon-phone"></i></span>
          <input
            type="text"
            v-model="accountNumber"
            placeholder="请输入账号"
            :class="{ 'input-error': accountError }"
            autocomplete="username"
          />
        </div>
        <span v-if="accountError" class="error-message">{{ accountError }}</span>
        <div class="input-group">
          <span class="input-icon"><i class="icon icon-lock"></i></span>
          <input
            type="password"
            v-model="userPassword"
            placeholder="请输入密码"
            :class="{ 'input-error': passwordError }"
            autocomplete="new-password"
          />
        </div>
        <span v-if="passwordError" class="error-message">{{ passwordError }}</span>
        <div class="input-group">
          <span class="input-icon"><i class="icon icon-lock"></i></span>
          <input
            type="password"
            v-model="userPassword2"
            placeholder="请再次输入密码"
            :class="{ 'input-error': password2Error }"
            autocomplete="new-password"
          />
        </div>
        <span v-if="password2Error" class="error-message">{{ password2Error }}</span>
        <button class="login-btn" type="submit" :disabled="isSubmitting">
          {{ isSubmitting ? '注册中...' : '注册' }}
        </button>
        <div class="login-bottom">
          已有账号？<router-link to="/login" class="register-link">立即登录</router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
const nickname = ref('')
const accountNumber = ref('')
const userPassword = ref('')
const userPassword2 = ref('')
const isSubmitting = ref(false)
const nicknameError = ref('')
const accountError = ref('')
const passwordError = ref('')
const password2Error = ref('')
const router = useRouter()

const validateForm = () => {
  nicknameError.value = nickname.value.trim() ? '' : '昵称不能为空'
  accountError.value = accountNumber.value.trim() ? '' : '账号不能为空'
  passwordError.value = userPassword.value.trim() ? '' : '密码不能为空'
  if (!passwordError.value && userPassword.value.length < 6) {
    passwordError.value = '密码长度不能少于6位'
  }
  password2Error.value = userPassword2.value.trim() ? '' : '请再次输入密码'
  if (!password2Error.value && userPassword.value !== userPassword2.value) {
    password2Error.value = '两次输入的密码不一致'
  }
  return !nicknameError.value && !accountError.value && !passwordError.value && !password2Error.value
}

const handleRegister = async () => {
  if (!validateForm()) return
  isSubmitting.value = true
  try {
    // 这里应调用实际注册接口
    await new Promise(resolve => setTimeout(resolve, 1200))
    // 假设注册成功
    router.replace('/login')
  } catch (e) {
    // 错误处理
  } finally {
    isSubmitting.value = false
  }
}
</script>

<style scoped>
:global(html, body, #app) {
  margin: 0;
  padding: 0;
  width: 100vw;
  height: 100vh;
  min-height: 100vh;
  background: linear-gradient(135deg, #e0e7ff 0%, #f8fafc 100%) !important;
  box-sizing: border-box;
}
.login-bg {
  min-height: 100vh;
  height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  padding: 0;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;
}
.login-card {
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 4px 32px rgba(37,99,235,0.10);
  padding: 48px 38px 36px 38px;
  width: 100%;
  max-width: 370px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.login-header {
  text-align: center;
  margin-bottom: 32px;
}
.login-logo img {
  width: 56px;
  height: 56px;
  margin-bottom: 10px;
}
.login-header h2 {
  font-size: 1.6em;
  color: #2563eb;
  margin-bottom: 8px;
  font-weight: bold;
}
.login-desc {
  color: #888;
  font-size: 1em;
  margin-bottom: 0;
}
.login-form {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.input-group {
  position: relative;
  display: flex;
  align-items: center;
  margin-bottom: 2px;
}
.input-icon {
  position: absolute;
  left: 12px;
  color: #b3b3b3;
  font-size: 1.1em;
}
.input-group input {
  width: 100%;
  padding: 10px 12px 10px 38px;
  border: 1px solid #e5e7eb;
  border-radius: 6px;
  font-size: 1em;
  outline: none;
  transition: border 0.2s;
  background: #f7faff;
}
.input-group input:focus {
  border: 1.5px solid #2563eb;
  background: #fff;
}
.input-error {
  border: 1.5px solid #e74c3c !important;
  background: #fff0f0 !important;
}
.error-message {
  color: #e74c3c;
  font-size: 0.95em;
  margin-bottom: 2px;
  margin-left: 2px;
}
.login-btn {
  width: 100%;
  background: linear-gradient(90deg, #2563eb 60%, #4f8cff 100%);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 12px 0;
  font-size: 1.1em;
  font-weight: bold;
  margin-top: 10px;
  margin-bottom: 6px;
  cursor: pointer;
  transition: background 0.2s, box-shadow 0.2s;
  box-shadow: 0 2px 8px rgba(37,99,235,0.08);
}
.login-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}
.login-btn:hover:not(:disabled) {
  background: linear-gradient(90deg, #1e40af 60%, #2563eb 100%);
}
.login-bottom {
  text-align: center;
  margin-top: 8px;
  color: #888;
  font-size: 0.98em;
}
.register-link {
  color: #2563eb;
  text-decoration: underline;
  margin-left: 4px;
  transition: color 0.2s;
}
.register-link:hover {
  color: #1e40af;
}
@media (max-width: 500px) {
  .login-card {
    padding: 28px 8px 18px 8px;
    max-width: 98vw;
  }
}
</style>
